package com.lyc.ssm.service.impl;

import com.lyc.ssm.dao.StudentMapper;
import com.lyc.ssm.dao.TeacherMapper;
import com.lyc.ssm.domain.Student;
import com.lyc.ssm.domain.Teacher;
import com.lyc.ssm.service.IStudentService;
import com.lyc.ssm.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ITeacherService teacherService;

    public int addStudent(Student student) {
        return studentMapper.insertSelective(student);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)//该方法抛出Exception的子类异常时就会回滚
//    @Transactional(noRollbackFor = RuntimeException.class)//该方法抛出指定RuntimeException（不受检查的异常）的子类异常时不会回滚
    public void addStudentWithTeacher(Student student, Teacher teacher) {
        teacherService.addTeacher(teacher);
        student.setTid(teacher.getTid());
//        int i=1/0;
        studentMapper.insertSelective(student);
    }
}
